home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / me_cd25.zip / MUTT2.ZIP / TEXTMODE.MUT < prev    next >
Text File  |  1992-11-09  |  9KB  |  326 lines

  1.   ;; textmode.mut : An electric text mode.
  2.   ;; C Durland    Public Domain
  3.  
  4.   ;; You will notice a lack of anything to do with sentences, outlining and
  5.   ;;   other useful text mode things.
  6.  
  7.   ;; Routines provided:
  8.   ;;   adjust-block (bound to M-J):  Format all lines in block.  Uses
  9.   ;;     (word-wrap) or fill-column as the right margin.
  10.   ;;   center-line (bound to M-S):  Center the text in the line that the
  11.   ;;     cursor is on.  With C-U, center n lines.  Uses (word-wrap) or
  12.   ;;     (screen-width) as right margin.
  13.   ;;   center-region (not bound):  Center all lines in a block.
  14.   ;;   underline-line (not bound):  Underline a line of text.  All nonspace
  15.   ;;     characters are underlined with dashes.
  16.  
  17.   ;;   forward-paragraph (bound to M-e):  Attempts to move to the end of a
  18.   ;;     paragraph.
  19.   ;;   backward-paragraph (bound to M-a):  Attempts to move to the begining
  20.   ;;     of a paragraph.
  21.   ;;   mark-paragraph (bound to M-h):  Attempts to make the region include
  22.   ;;     the paragraph the cursor is in.  Put mark at beginning of this
  23.   ;;     paragraph, point at end.  If between paragraphs, mark the next
  24.   ;;     one.
  25.   ;;   cut-paragraph: cut to end of paragraph.
  26.   ;;   backward-cut-paragraph: cut back to start of paragraph.
  27.  
  28.  
  29. (const
  30.   FILL-COLUMN  72    ; Column to word wrap at (0 means no wrapping)
  31.   TAB-SIZE    0    ; Tab size (0 means use the TAB character)
  32. )
  33.  
  34. (small-int fill-column)
  35.  
  36. (defun
  37.   text-mode
  38.   {
  39.     (clear-modes)
  40.  
  41.     (bind-local-key "newline-and-indent" "C-M")
  42.     (bind-local-key "adjust-block"     "M-J")
  43.     (bind-local-key "center-line"     "M-S")
  44.  
  45.     (bind-local-key "backward-paragraph" "M-a")
  46.     (bind-local-key "forward-paragraph"  "M-e")
  47.     (bind-local-key "mark-paragraph"     "M-h")
  48.  
  49.     (major-mode "Text")
  50.  
  51.     (tab-stops TAB-SIZE)
  52.     (word-wrap FILL-COLUMN)
  53.     (text-fill-column FILL-COLUMN)
  54.  
  55.     (if (pgm-exists "text-mode-hook") (floc "text-mode-hook"()))
  56.   }
  57.   text-fill-column    ;; set or get the fill-column
  58.   {
  59.     (if (!= 0 (nargs)) (fill-column (arg 0)))
  60.     fill-column
  61.   }
  62. )
  63.  
  64. (include me2.h)
  65. (include wspace.mut)
  66.  
  67. (defun
  68.   center-line  ; Center line the cursor is on.  With arg, centers n lines.
  69.   {
  70.     (int n indent wrap-col)
  71.  
  72.     (if (== (wrap-col (word-wrap)) 0) (wrap-col (screen-width)))
  73.     (beginning-of-line)
  74.     (for (n (arg-prefix)) (< 0 n) (-= n 1)
  75.     {
  76.       (delete-whitespace)
  77.       (end-of-line)(indent (/ (+ 1 (- wrap-col (current-column))) 2))
  78.       (beginning-of-line)(to-col indent)
  79.       (forward-line 1)        ; move to the next line
  80.     })
  81.   }
  82.   center-region        ; Center all the lines in a region.
  83.   {
  84.     (byte type)(small-int left-edge width height)(int size)    ;; RegionInfo
  85.  
  86.     (region-stats (loc type) THE-DOT THE-MARK TRUE)
  87.  
  88.     (arg-prefix height)(center-line)
  89.   }
  90. )
  91.  
  92. (defun
  93.   underline-line    ;; underline (with dashes) all text on a line
  94.   {
  95.     (int col)
  96.  
  97.     (end-of-line)(open-line)(beginning-of-line)
  98.     (while (not (looking-at "$"))
  99.     {
  100.       (if (not (is-space))    ;; underline this character
  101.       {
  102.     (col (current-column))
  103.     (forward-line 1)(end-of-line)
  104.     (to-col col)(insert-text "-")
  105.     (forward-line -1)(current-column col)
  106.       })
  107.       (next-character)
  108.     })
  109.   }
  110. )
  111.  
  112. (include block.mut)    ;; for (delete-region-as-block)
  113.  
  114.     ;; Format a block of lines - those lines between the dot and mark
  115.     ;;   inclusive.
  116.     ;; Formats the block ragged right.  With argument, justifies.
  117.     ;; See adjust.mut for details.
  118. (defun
  119. ;  adjust-block
  120. ;  {
  121. ;    (byte type)(small-int left-edge width height)(int size)    ;; RegionInfo
  122. ;
  123. ;    (region-stats (loc type) THE-DOT THE-MARK TRUE)
  124. ;
  125. ;    (msg "Formatting ...")
  126. ;    (adjust-lines height
  127. ;      (if (== 0 (word-wrap)) fill-column (word-wrap))
  128. ;      (arg-flag))
  129. ;    (msg "Formatted.")
  130. ;  }
  131.     ;; Another version of adjust-block.  I did this one because
  132.     ;;   adjust-lines is pretty slow and undo makes it really slow.  So
  133.     ;;   I do the formating in a buffer with no undo and take the hit
  134.     ;;   of moving the text from buffer to buffer.
  135.   adjust-block
  136.   {
  137.     (int text-buffer temp-buffer bag-id wrap-col)
  138.  
  139.     (wrap-col (if (== 0 (word-wrap)) fill-column (word-wrap)))
  140.  
  141.     (text-buffer (current-buffer))
  142.     (delete-region-as-block)
  143.  
  144.     (current-buffer (temp-buffer (create-buffer "")))
  145.     (insert-bag CUT-BUFFER)
  146.     (beginning-of-buffer)
  147.  
  148.     (msg "Formatting ...")
  149.     (adjust-lines 10000 wrap-col (arg-flag))        ;; !!!ick
  150.  
  151.     (beginning-of-buffer)(set-mark)(end-of-buffer)
  152.     (append-to-bag (bag-id (create-bag)) APPEND-REGION)
  153.  
  154.     (msg "Formatted.")
  155.  
  156.     (current-buffer text-buffer)
  157.     (insert-bag bag-id)
  158.  
  159.     ; clean up
  160.     (free-buffer temp-buffer)(free-bag bag-id)
  161.   }
  162. )
  163.  
  164. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  165. ;;;;;;;; Paragraphs ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  166. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  167.  
  168. (const
  169.   between-paragraphs        0
  170.   at-start-of-paragraph        1
  171.   on-first-line-of-paragraph    2
  172.   in-paragraph            3
  173.   on-last-line-of-paragraph    4
  174.   at-end-of-paragraph        5
  175. )
  176.  
  177. ;; Move forward to end of paragraph.  With arg, do it arg times.
  178. ;; If at end of paragraph, move to the end of the next one.
  179.  
  180. (defun
  181.   forward-paragraph
  182.   {
  183.     (int col)
  184.  
  185.     (switch (where-in-paragraph)
  186.       between-paragraphs
  187.       {
  188.       (label above-paragraph)
  189.     (skip-forward-blank-lines)
  190.     (forward-line 1)        ;; skip over first line of paragraph
  191.     (if (looking-at '\ *$') { (forward-line -1)(end-of-line)(done) })
  192.       }
  193.       at-start-of-paragraph        ;; skip over first line of paragraph
  194.         (forward-line 1)
  195.       on-first-line-of-paragraph    ;; skip over first line of paragraph
  196.         (forward-line 1)
  197.       on-last-line-of-paragraph   { (end-of-line)(done) }
  198.       at-end-of-paragraph      { (forward-line 1)(goto above-paragraph) }
  199.     )
  200.     (col (get-indent))
  201.     (while TRUE
  202.     {
  203.       (if (not (forward-line 1)) (break))    ;; at end of buffer
  204.       (if (looking-at '\ *$') { (forward-line -1)(break) })
  205.       (skip-whitespace)
  206.       (if (!= col (current-column)) { (forward-line -1)(break) })
  207.     })
  208.     (end-of-line)
  209.   }
  210. )
  211.  
  212. ;; Move backward to start of paragraph.  With arg, do it arg times.
  213. ;; If at start of paragraph, move to start of the one above.
  214.  
  215. (defun
  216.   backward-paragraph
  217.   {
  218.     (int col)
  219.  
  220.     (switch (where-in-paragraph)
  221.       between-paragraphs      (skip-backward-blank-lines)
  222.       at-start-of-paragraph
  223.         { (forward-line -1)(skip-backward-blank-lines) }
  224.       on-first-line-of-paragraph  { (beginning-of-line) (done) }
  225.     )
  226.     (col (get-indent))
  227.     (while TRUE
  228.     {
  229.       (if (not (forward-line -1)) (break))    ;; top of buffer
  230.       (if (looking-at '\ *$') { (forward-line 1)(break) })
  231.       (skip-whitespace)
  232.       (if (!= col (current-column)) (break) )
  233.     })
  234.     (beginning-of-line)
  235.   }
  236. )
  237.  
  238. (defun
  239.     ;; Put mark at beginning of this paragraph, point at end.
  240.     ;; If between paragraphs, mark the next one.
  241.   mark-paragraph
  242.   {
  243.     (forward-paragraph)
  244.     (set-mark)
  245.     (backward-paragraph)
  246.     (swap-marks)
  247.     (msg "Paragraph marked")
  248.   }
  249.   cut-paragraph        ;; Cut to end of paragraph.
  250.   {
  251.     (set-mark)(forward-paragraph)(cut-region)
  252.   }
  253.   backward-cut-paragraph    ;; Cut back to start of paragraph.
  254.   {
  255.     (set-mark)(backward-paragraph)(cut-region)
  256.   }
  257. )
  258.  
  259.  
  260. (defun
  261.     ;;   Try to figure out where the point is in a paragraph by looking at
  262.     ;; the lines above and below the point.
  263.   where-in-paragraph HIDDEN
  264.   {
  265.     (int above below point col)
  266.  
  267.     (col (current-column))
  268.     (beginning-of-line)
  269.     (if (looking-at '\ *$') { between-paragraphs (done) })
  270.     ;; looking at text
  271.     (point (get-indent))
  272.     ;; look at the line above
  273.     (if (not (forward-line -1))        ;; at beginning of buffer
  274.     {
  275.     (label on-first-line-of-paragraph)
  276.       (if (== col 1) at-start-of-paragraph on-first-line-of-paragraph)
  277.       (done)
  278.     })
  279.     (if (looking-at '\ *$')
  280.       { (forward-line 1) (goto on-first-line-of-paragraph) })
  281.     (above (get-indent))
  282.         ;; look at the following line
  283.     (if (forwar